#include "SkillBullet.h"
#include "Character/SkillPart.h"
#include "base/core/log.h"
#include "base/core/com_define.h"

SkillBullet::SkillBullet() 
{
	m_vBullet.clear();
}
SkillBullet::~SkillBullet()
{

}

void SkillBullet::refreshData()
{
	SkillBase::refreshData();
	m_vBullet.clear();
}

//重写这个伤害准备状态，子弹有飞行效果
void SkillBullet::DamageState(uint32_t skillId)
{
	Creature* pCreate = nullptr;
	float distance = 0.0f;
	stBulletInfo stInfo;
	stSkillInfo* pSkill = m_pPart->GetSkillInfo(skillId);
	MMOLOG_PROCESS_ERROR(pSkill != nullptr);

	m_pPart->KillTimedo(m_nGlobelID);
	m_curState = SkillState_Fly;

	//先找技能目标，分俩种情况，一中是单体目标，一种是以单体目标为圆心，按范围找指定数量目标
	MMOLOG_PROCESS_ERROR(m_vTargetCids.size());
	pCreate = m_pPart->GetCreature(m_vTargetCids[0]);
	//MMOLOG_PROCESS_ERROR(pCreate);
	if (!pCreate)
	{
		m_pPart->KillTimedo(m_nGlobelID);
		m_pPart->RemoveSkillFightInfo(m_nGlobelID);
		return;
	}
	//找目标
	//cList  tmpList;
	//m_pPart->FindCreatures(pSkill->_pSkillmeta, m_vTargetCids[0], pCreate->GetPos(), m_curDamageCount, m_focoTime, tmpList);
	
	stInfo._targetCid = m_vTargetCids[0];
	stInfo._pos = pCreate->GetPos();
	stInfo._dir = pCreate->GetPos() - m_pPart->GetMasterPos();

	m_vBullet.push_back(stInfo);


	distance = point3Length(m_pPart->GetMasterPos(), pCreate->GetPos());

	//启动定时器来计算子弹位置,定时器和人物行走一致
	m_pPart->SetTimedo(m_nGlobelID, distance*1000*1000 / pSkill->_pSkillmeta->bulledSpeed, 1);
	return;
Exit0:
	m_pPart->KillTimedo(m_nGlobelID);
	m_pPart->RemoveSkillFightInfo(m_nGlobelID);
	return ;
}

void SkillBullet::OnFly(uint32_t skillId)
{
	Creature * pCreate = nullptr;
	float distance = 0.0f;
	stSkillInfo* pSkill = m_pPart->GetSkillInfo(skillId);
	MMOLOG_PROCESS_ERROR(pSkill != nullptr);
	MMOLOG_PROCESS_ERROR(m_vBullet.size() > 0);

	pCreate = m_pPart->GetCreature(m_vBullet[0]._targetCid);
	MMOLOG_PROCESS_ERROR(pCreate);

	m_pPart->KillTimedo(m_nGlobelID);
	if (m_vBullet[0]._pos==pCreate->GetPos())
	{
		m_curState = SkillState_damage;
		OnDamageTimedo(skillId);
	}
	else
	{
		distance = point3Length(m_vBullet[0]._pos, pCreate->GetPos());
		m_vBullet[0]._dir = pCreate->GetPos() - m_vBullet[0]._pos;
		m_vBullet[0]._pos = pCreate->GetPos();

		//启动定时器来计算子弹位置,定时器和人物行走一致
		m_pPart->SetTimedo(m_nGlobelID, distance * 1000*1000 / pSkill->_pSkillmeta->bulledSpeed, 1);
	}
	return;
Exit0:
	m_pPart->KillTimedo(m_nGlobelID);
	m_pPart->RemoveSkillFightInfo(m_nGlobelID);
	return;
}


void SkillBullet::OnDamageTimedo(uint32_t skillId)
{
	Creature * pCreature = nullptr;
	cList tagetlist;
	stSkillInfo* pSkill = m_pPart->GetSkillInfo(skillId);
	MMOLOG_PROCESS_ERROR(pSkill != nullptr);
	m_pPart->KillTimedo(m_nGlobelID);
	MMOLOG_PROCESS_ERROR(m_vBullet.size() > 0);
	MMOLOG_PROCESS_ERROR(pSkill->_pSkillmeta != nullptr);

	pCreature = m_pPart->GetCreature(m_vBullet[0]._targetCid);
	if (pCreature)
	{
		
		if (pSkill->_pSkillmeta->rangeType == Range_pos_mine_target)
		{
			tagetlist.push_back(m_vBullet[0]._targetCid);
		}
		else
		{
			m_pPart->FindCreatures(pSkill->_pSkillmeta, m_pPart->GetMasterPos(), m_skillDir, m_vBullet[0]._targetCid, pCreature->GetPos(), m_curDamageCount, m_focoTime, tagetlist);
		}		
			//打中了，则算伤害
		m_pPart->SkillResult(pSkill->_Id, pSkill->_pSkillmeta, m_curDamageCount, m_focoTime, tagetlist);
	}

	if (pSkill->_pSkillmeta->damageTime.size() > m_curDamageCount + 1)
	{
		m_curDamageCount++;
		m_pPart->SetTimedo(m_nGlobelID, pSkill->_pSkillmeta->damageTime[m_curDamageCount], 1);
	}
	else
	{
	
		//if (m_vSkillId.size()>1)
		//{
		//	OnNextSkillState(skillId);
		//	
		//}
		//else
		{
			m_pPart->KillTimedo(m_nGlobelID);
			m_pPart->RemoveSkillFightInfo(m_nGlobelID);
		}
	}
	return;
Exit0:
	m_pPart->KillTimedo(m_nGlobelID);
	m_pPart->RemoveSkillFightInfo(m_nGlobelID);
	return;
}

void SkillBullet::OnNextSkillState(uint32_t skillId)
{
	Creature * pCreate = nullptr;
	float distance = 0.0f;
	m_nStep++;
	m_curState = SkillState_NextSkill;
	const SkillCfg* pConfig = nullptr;
	MMOLOG_PROCESS_ERROR(m_nStep <= m_nStopStep && m_nStep <= m_vSkillId.size()); //步数走完，正常退出
	m_curDamageCount = 0;
	m_focoTime = 0;
	//重置一些变量,然后设置延迟伤害时间
	pConfig = g_GetSkillMgr()->GetSkillCfg(m_vSkillId[m_nStep - 1]);
	MMOLOG_PROCESS_ERROR(pConfig);
	MMOLOG_PROCESS_ERROR(m_vBullet.size() > 0);
	pCreate = m_pPart->GetCreature(m_vBullet[0]._targetCid);
	MMOLOG_PROCESS_ERROR(pCreate != nullptr);
	m_pPart->KillTimedo(m_nGlobelID);

	distance = point3Length(m_pPart->GetMasterPos(), pCreate->GetPos());
	//启动定时器来计算子弹位置,定时器和人物行走一致
	if (pConfig->damageTime.size()>0)
	{
		m_pPart->SetTimedo(m_nGlobelID, distance * 1000 * 1000 / pConfig->bulledSpeed + pConfig->damageTime[0], 1);
	}
	else
	{
		m_pPart->SetTimedo(m_nGlobelID, distance * 1000 * 1000 / pConfig->bulledSpeed, 1);
	}	
	return;
Exit0:
	m_pPart->KillTimedo(m_nGlobelID);
	m_pPart->RemoveSkillFightInfo(m_nGlobelID);
}